package com.lease.web.admin.controller.system;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lease.common.result.Result;
import com.lease.model.entity.SystemPost;
import com.lease.model.entity.SystemUser;
import com.lease.model.enums.BaseStatus;
import com.lease.web.admin.service.SystemPostService;
import com.lease.web.admin.service.SystemUserService;
import com.lease.web.admin.vo.system.user.SystemUserItemVo;
import com.lease.web.admin.vo.system.user.SystemUserQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.codec.digest.DigestUtils;
import org.bouncycastle.crypto.digests.MD5Digest;
import org.bouncycastle.jcajce.provider.digest.MD5;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


@Tag(name = "后台用户信息管理")
@RestController
@RequestMapping("/admin/system/user")
public class SystemUserController {
    @Autowired
    private  SystemUserService systemUserService;
    @Autowired
    private  SystemPostService systemPostService;


    @Operation(summary = "根据条件分页查询后台用户列表")
    @GetMapping("page")
    public Result<IPage<SystemUserItemVo>> page(@RequestParam long current, @RequestParam long size, SystemUserQueryVo queryVo) {
        Page<SystemUserItemVo> page = new Page<>(current, size);

        IPage<SystemUserItemVo> IPage =  systemUserService.getAllSystemUserItem(page, queryVo);

        return Result.ok(IPage);
    }

    @Operation(summary = "根据ID查询后台用户信息")
    @GetMapping("getById")
    public Result<SystemUserItemVo> getById(@RequestParam Long id) {
        SystemUserItemVo systemUserItemVo = new SystemUserItemVo();

        LambdaQueryWrapper<SystemUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SystemUser::getId, id);
        SystemUser systemUser = systemUserService.getOne(queryWrapper);

        BeanUtils.copyProperties(systemUser,systemUserItemVo);

        LambdaQueryWrapper<SystemPost> postQueryWrapper = new LambdaQueryWrapper<>();
        postQueryWrapper.eq(SystemPost::getId, systemUser.getPostId());
        SystemPost systemPost = systemPostService.getOne(postQueryWrapper);

        systemUserItemVo.setPostName(systemPost.getName());

        return Result.ok(systemUserItemVo);
    }

    @Operation(summary = "保存或更新后台用户信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SystemUser systemUser) {
        //密码判空保护
        if(StringUtils.hasText(systemUser.getPassword())){


        String s = DigestUtils.md5Hex(systemUser.getPassword());
        systemUser.setPassword(s);
        systemUserService.saveOrUpdate(systemUser);

        }
        return Result.ok();
    }

    @Operation(summary = "判断后台用户名是否可用")
    @GetMapping("isUserNameAvailable")
    public Result<Boolean> isUsernameExists(@RequestParam String username) {
        LambdaQueryWrapper<SystemUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SystemUser::getUsername, username);
        long count = systemUserService.count(queryWrapper);

        return Result.ok(count == 0);
    }

    @DeleteMapping("deleteById")
    @Operation(summary = "根据ID删除后台用户信息")
    public Result removeById(@RequestParam Long id) {
        systemUserService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "根据ID修改后台用户状态")
    @PostMapping("updateStatusByUserId")
    public Result updateStatusByUserId(@RequestParam Long id, @RequestParam BaseStatus status) {
        systemUserService.update(
                new LambdaUpdateWrapper<SystemUser>().eq(SystemUser::getId, id)
                        .set(SystemUser::getStatus, status)
        );
        return Result.ok();
    }
}
